package com.alander.nexusgo.itinerary.controller;

import com.alander.nexusgo.common.model.dto.ItineraryCollaboratorListDTO;
import com.alander.nexusgo.common.model.vo.ItineraryCollaboratorVO;
import com.alander.nexusgo.common.response.Result;
import com.alander.nexusgo.framework.web.starter.controller.BaseController;
import com.alander.nexusgo.itinerary.model.entity.ItineraryCollaborator;
import com.alander.nexusgo.itinerary.service.ItineraryCollaboratorService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.alander.nexusgo.framework.mysql.starter.page.PageUtils.getPageInfo;
import static com.alander.nexusgo.framework.mysql.starter.page.PageUtils.startPage;

/**
* 协作行程成员管理
*/
@RestController
@RequestMapping("/api/itinerary-collaborators")
public class ItineraryCollaboratorController extends BaseController {
    
    @Autowired
    private ItineraryCollaboratorService itineraryCollaboratorService;
    
    /**
     * 添加协作者
     */
    @PostMapping
    public Result<ItineraryCollaboratorVO> create(@RequestBody ItineraryCollaborator itineraryCollaborator) {
        try {
            ItineraryCollaboratorVO result = itineraryCollaboratorService.createCollaborator(itineraryCollaborator, getUserId());
            return success(result);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }
    
    /**
     * 获取协作者详情
     */
    @GetMapping("/{id}")
    public Result<ItineraryCollaboratorVO> get(@PathVariable(value = "id") Long id) {
        ItineraryCollaboratorVO itineraryCollaborator = itineraryCollaboratorService.getCollaboratorById(id);
        if (itineraryCollaborator == null) {
            return fail("协作者不存在");
        }
        return success(itineraryCollaborator);
    }
    
    /**
     * 更新协作者信息
     */
    @PutMapping("/{id}")
    public Result<ItineraryCollaboratorVO> update(@PathVariable(value = "id") Long id, @RequestBody ItineraryCollaborator itineraryCollaborator) {
        try {
            ItineraryCollaboratorVO result = itineraryCollaboratorService.updateCollaborator(id, itineraryCollaborator, getUserId());
            return success(result);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }
    
    /**
     * 删除协作者
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable(value = "id") Long id) {
        try {
            boolean result = itineraryCollaboratorService.deleteCollaborator(id, getUserId());
            if (result) {
                return success();
            } else {
                return fail("删除失败");
            }
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }
    
    /**
     * 分页查询协作者列表
     */
    @GetMapping
    public PageInfo<ItineraryCollaboratorVO> list(@RequestBody ItineraryCollaboratorListDTO itineraryCollaboratorListDTO) {
        startPage(itineraryCollaboratorListDTO.getPageNum(),itineraryCollaboratorListDTO.getPageSize());
        List<ItineraryCollaboratorVO> itineraryCollaboratorVOList = itineraryCollaboratorService.listCollaborators(itineraryCollaboratorListDTO);
        return getPageInfo(itineraryCollaboratorVOList);
    }
}